package com.whj.apidriver.controller;

import com.whj.apidriver.service.OrderService;
import com.whj.internalcommon.constant.CommonStatusEnum;
import com.whj.internalcommon.constant.IdentityConstant;
import com.whj.internalcommon.dto.ResponseResult;
import com.whj.internalcommon.dto.TokenResult;
import com.whj.internalcommon.request.OrderRequest;
import com.whj.internalcommon.utils.JwtUtils;
import com.whj.serviceorder.entity.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: wanghaijun
 * @Date: 2023/2/26 - 02 - 26 - 14:25 
 * @Description: com.whj.apidriver.controller
 */

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 司机去接乘客
     *
     * @param orderRequest
     * @return
     */
    @PostMapping("/to-pick-up-passenger")
    public ResponseResult changeStatus(@RequestBody OrderRequest orderRequest){
        return orderService.toPickUpPassenger(orderRequest);
    }

    /**
     * 司机到达接乘客的位置
     *
     * @param orderRequest
     * @return
     */
    @PostMapping("/arrived-departure")
    public ResponseResult arriveDeparture(@RequestBody OrderRequest orderRequest){
        return orderService.arriveDeparture(orderRequest);
    }

    /**
     * 司机接到乘客
     *
     * @param orderRequest
     * @return
     */
    @PostMapping("/pick-up-passenger")
    public ResponseResult pickUpPassenger(@RequestBody OrderRequest orderRequest){
        return orderService.pickUpPassenger(orderRequest);
    }

    /**
     * 乘客达到目的地,订单中止
     *
     * @param orderRequest
     * @return
     */
    @PostMapping("/passenger-getoff")
    public ResponseResult passengerGetOff(@RequestBody OrderRequest orderRequest){
        return orderService.passengerGetOff(orderRequest);
    }

    /**
     * 乘客取消订单
     *
     * @param orderId 订单id
     * @return
     */
    @PostMapping("/cancel")
    public ResponseResult cancel(@RequestParam Long orderId){
        return orderService.cancel(orderId);
    }

    @GetMapping("/detail")
    public ResponseResult<OrderInfo> detail(Long orderId){
        return orderService.detail(orderId);
    }
    @GetMapping("/current")
    public ResponseResult<OrderInfo> currentOrder(HttpServletRequest httpServletRequest){
        String authorization = httpServletRequest.getHeader("Authorization");
        TokenResult tokenResult = JwtUtils.parseToken(authorization);
        String identity = tokenResult.getIdentity();
        if (!identity.equals(IdentityConstant.DRIVER_IDENTITY)){
            return ResponseResult.fail(CommonStatusEnum.TOKEN_ERROR.getCode(),CommonStatusEnum.TOKEN_ERROR.getValue());
        }
        String phone = tokenResult.getPhone();

        return orderService.currentOrder(phone,IdentityConstant.DRIVER_IDENTITY);
    }

}
